<?php

namespace Api\Member\AuthManager;

use Illuminate\Http\Request;
use Dingo\Api\Routing\Route;
use Dingo\Api\Auth\Provider\Authorization;
use Dingo\Api\Contract\Auth\Provider;

class JWTAuthProvider extends Authorization implements Provider
{
    /**
     * 构造方法
     */
    public function __construct(Open\ManagerRepository $ManagerRepository)
    {
        $this->ManagerRepository = $ManagerRepository;
    }

    /**
     * 校验
     * 
     * @param  \Illuminate\Http\Request $request
     * @param  \Dingo\Api\Routing\Route   $route
     * @return void
     */
    public function authenticate(Request $request, Route $route)
    {
        $this->validateAuthorizationHeader($request);

        try {
            return $this->ManagerRepository->authenticateByToken();
        } catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
            error('unauthorized');
        } catch (\Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
            error('unauthorized');
        } catch (\Tymon\JWTAuth\Exceptions\JWTException $e) {
            error('unauthorized');
        } catch (\AuthManager\Exceptions\Exception $e) {
            error('unauthorized');
        }
    }

    /**
     * 校验方法
     * 
     * @return string
     */
    public function getAuthorizationMethod()
    {
        return 'bearer';
    }
}
